<template>
  <div id="body">
    <el-main>
      <div class="contentText">
        <div class="content1">
          <div id="upload" class="upload">
            <el-upload
              class="avatar-uploader"
              :show-file-list="false"
              :before-upload="beforeupload"
              :expand-on-click-node="false"
              drag
              action=""
            >
              <img
                v-if="imgSrc"
                :src="imgSrc"
                style="height: 100%; float: left"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </div>
        <div class="content2">
          <el-button
            type="primary"
            style="margin: 10px 0; z-index: 99999"
            @click="() => append()"
            >{{ $t("operating.add") }}</el-button
          >
          <div style="max-height: 190px; overflow-y: auto">
            <el-tree
              class="spanBtn"
              :data="this.areaData"
              ref="tree"
              current-node-key
              highlight-current
              :props="this.defaultProps"
              @node-click="nodeClick"
              node-key="id"
              :default-expand-all="true"
              :expand-on-click-node="false"
            >
              <span class="custom-tree-node" slot-scope="{ node, data }">
                <span>{{ node.label }}</span>
                <!-- <span> -->
                <el-button type="text" size="mini" @click="() => edit(data)">
                  <i class="el-icon-edit" style="font-size: 14px"></i>
                </el-button>
                <el-button
                  type="text"
                  size="mini"
                  @click="() => remove(node, data)"
                >
                  <i class="el-icon-delete"></i>
                </el-button>
                <!-- </span> -->
              </span>
            </el-tree>
          </div>
          <div>
            <el-input
              v-model="imgSrc"
              style="width: 600px"
              :disabled="true"
              type="hidden"
            ></el-input>
            <el-form
              :label-position="labelPosition"
              label-width="120px"
              ref="formValue"
              :rules="rules"
              :model="formValue"
              @submit.navite="onSubmit"
            >
              <el-form-item :label="$t('map.mapName')" prop="mapName">
                <el-input
                  v-model="formValue.mapName"
                  :disabled="true"
                ></el-input>
              </el-form-item>

              <el-form-item label="FUSER:" prop="lcid">
                <el-select
                  v-model="formValue.lcid"
                  filterable
                  :placeholder="$t('operating.pleaseChosen')"
                  style="width: 150px"
                >
                  <el-option
                    v-for="item in this.fuserList"
                    :key="item.fuserLcid"
                    :label="item.fuserName"
                    :value="item.fuserLcid"
                  ></el-option>
                </el-select>
              </el-form-item>

              <el-form-item
                :label="$t('map.layerId')"
                prop="layerId"
                :placeholder="$t('operating.pleaseWrite')"
              >
                <el-input v-model="formValue.layerId"></el-input>
              </el-form-item>

              <el-form-item
                :label="$t('map.mapPixelX')"
                prop="mapPixelX"
                :placeholder="$t('operating.pleaseWrite')"
              >
                <el-input v-model="formValue.mapPixelX"></el-input>
              </el-form-item>

              <el-form-item
                :label="$t('map.mapPixelY')"
                prop="mapPixelY"
                :placeholder="$t('operating.pleaseWrite')"
              >
                <el-input v-model="formValue.mapPixelY"></el-input>
              </el-form-item>

              <el-form-item
                :label="$t('map.mapActualX')"
                prop="mapActualX"
                :placeholder="$t('operating.pleaseWrite')"
              >
                <el-input v-model="formValue.mapActualX"></el-input>
              </el-form-item>

              <el-form-item
                :label="$t('map.mapActualY')"
                prop="mapActualY"
                :placeholder="$t('operating.pleaseWrite')"
              >
                <el-input v-model="formValue.mapActualY"></el-input>
              </el-form-item>

              <el-form-item :label="$t('map.mapOriginX')" prop="mapOriginX">
                <el-input
                  v-model="formValue.mapOriginX"
                  type="number"
                ></el-input>
              </el-form-item>

              <el-form-item :label="$t('map.mapOriginY')" prop="mapOriginY">
                <el-input
                  v-model="formValue.mapOriginY"
                  type="number"
                ></el-input>
              </el-form-item>

              <el-form-item :label="$t('operating.remark')" prop="remark">
                <el-input v-model="formValue.remark"></el-input>
              </el-form-item> 

              <el-form-item class="submitBtn">
                <el-button type="primary" @click="onSubmit('formValue')">{{
                  $t("map.save")
                }}</el-button>
              </el-form-item>

              <!--是否启用-->
              <el-form-item class="isEnable" :label="$t('map.isEnable')">
                <el-switch
                  v-model="switchVal"
                  inactive-value="0"
                  active-value="1"
                  active-color="#ffc000"
                  inactive-color="#dcdfe6"
                  @change="isSwitch"
                ></el-switch>
              </el-form-item>
            </el-form>
          </div>
        </div>
        <!--编辑弹出框-->
        <el-dialog
          :title="$t('operating.handle')"
          :close-on-click-modal="false"
          :visible.sync="editVisible"
          width="30%"
        >
          <el-form
            ref="deptForm"
            :model="deptForm"
            label-width="120px"
            :rules="rules"
          >
            <!--父级名称-->
            <el-form-item :label="$t('map.ParentNode')">
              <el-input
                v-model="deptForm.ParentNode"
                style="max-width: 215px"
                :disabled="true"
              ></el-input>
            </el-form-item>
            <!--子级名称-->
            <el-form-item :label="$t('map.childenNode')" prop="childenNode">
              <el-input
                v-model="deptForm.childenNode"
                style="max-width: 215px"
              ></el-input>
            </el-form-item>
          </el-form>
          <!--操作-->
          <span slot="footer" class="dialog-footer">
            <el-button
              @click="saveEdit('deptForm')"
              type="primary"
              class="edit_confirm"
              >{{ $t("operating.determine") }}</el-button
            >
            <el-button @click="editVisible = false">{{
              $t("operating.cancel")
            }}</el-button>
          </span>
        </el-dialog>
        <el-dialog
          :title="$t('operating.prompt')"
          :close-on-click-modal="false"
          :visible.sync="delVisible"
          width=""
        >
          <div class="del-dialog-cnt">{{ $t("operating.deletePrompt") }}</div>
          <span slot="footer" class="dialog-footer">
            <el-button
              @click="delMapTree"
              type="primary"
              class="delete_confirm"
              >{{ $t("operating.determine") }}</el-button
            >
            <el-button @click="delVisible = false">{{
              $t("operating.cancel")
            }}</el-button>
          </span>
        </el-dialog>
        <el-dialog
          :title="$t('operating.prompt')"
          :close-on-click-modal="false"
          :visible.sync="defaultMapVisible"
          width=""
        >
          <div class="del-dialog-cnt">{{ $t("map.defaultMap") }}</div>
          <span slot="footer" class="dialog-footer">
            <el-button
              @click="defaultMap"
              type="primary"
              class="delete_confirm"
              >{{ $t("operating.determine") }}</el-button
            >
            <el-button @click="defaultMapVisible = false">{{
              $t("operating.cancel")
            }}</el-button>
          </span>
        </el-dialog>
      </div>
    </el-main>
  </div>
</template>
 
<script>
// import { baseUrl } from '../../utils/global';
import Cookie from "js-cookie";
export default {
  data() {
    return {
      mapId: "", //选中的地图id
      imgSrc: "", //地图路径
      labelPosition: "right", //表单label文字对齐方式
      formValue: {
        mapurl: "", //地图地址
        mapName: "", //地图名称
        lcid: "", //fuserid
        layerId: null, //层号
        mapPixelX: null, //像素宽度
        mapPixelY: null, //像素高度
        mapActualY: null, //实际长度
        mapActualX: null, //实际宽度
        mapOriginX: null,
        mapOriginY: null,
        remark: "", //地图备注
      },
      switchVal: "0", //是否启用
      editVisible: false, //增加地图树弹窗是否显示
      delVisible: false, //删除地图树弹窗是否显示
      defaultMapVisible: false,
      editEnable: true, //点击的编辑or添加
      areaData: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      file: new FormData(), //地图文件
      //表单验证规则
      rules: {},
      deptForm: { ParentNode: "", childenNode: "" },
      nodeName: "",
      parentId: 0,
      fuserList: "",
      idxMap: "",
      idxNode: "",
      // isActive: false,//输入框是否禁用
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.getAreaTree();
      this.getFuserList();
    },

    //获取地图树数据
    getAreaTree() {
      this.$api.mapmanage.areaTree().then((res) => {
        let RES = this.responseUse(res);
        this.areaData = RES;
        // let that = this;
        // this.$nextTick(function(){
        //   this.$data.areaData.forEach(row => {
        //     console.log(row);
        //     if(row.isDefault ===1){
        //       that.$refs.tree.setCurrentKey(Number(row.id));
        //       that.$refs.tree.store.nodesMap[Number(row.id)].expanded = true;
        //     }
        //   })
        // })
      });
    },
    //获取Fuser数据
    getFuserList() {
      this.$api.mapmanage.fuserList().then((res) => {
        let RES = this.responseUse(res);
        this.fuserList = RES;
      });
    },
    //是否有默认地图
    isSwitch(val) {
      console.log(val);
      this.switchVal = val;
    },
    /*****************************************************************************/
    // 树节点被点击时调用
    nodeClick(draggingNode, dropNode, ev) {
      let that = this;
      if (this.$refs["formValue"]) {
        this.$refs["formValue"].resetFields();
      }
      // 可将地图的ID设置为树结构的ID，通过点击节点，来切换地图
      // that.v_MyMessage("选择的树分支ID：" + draggingNode.id, "success");
      this.mapId = draggingNode.id;
      this.formValue.mapName = draggingNode.label;
      this.nodeName = draggingNode.label;
      if (draggingNode.parentId) {
        this.parentId = draggingNode.parentId;
      }
      this.$api.mapmanage
        .mapInfo("/mapInfo/findById?idx=" + this.mapId)
        .then((res) => {
          let RES = this.responseUse(res);
          if (!RES.mapPath) {
            this.imgSrc = "";
          } else {
            //   this.imgSrc = baseUrl + RES.mapPath;
          }
          // this.formValue={
          //   mapurl: RES.mapPath,
          //   mapName: RES.nodeName, //地图名称
          //   lcid: RES.lcid, //fuserid
          //   layerId: RES.layerId, //层号
          //   mapPixelX: RES.mapPixelX, //像素宽度
          //   mapPixelY: RES.mapPixelY, //像素高度
          //   mapActualY: RES.mapActualY, //实际长度
          //   mapActualX: RES.mapActualX, //实际宽度
          //   mapOriginX: RES.mapOriginX,
          //   mapOriginY: RES.mapOriginY,
          //   remark:''
          // };
          if (
            RES.mapPath != "" &&
            RES.mapPath != undefined &&
            RES.mapPath != null
          ) {
            this.formValue["mapurl"] = RES.mapPath;
          }
          if (
            RES.nodeName != "" &&
            RES.nodeName != undefined &&
            RES.nodeName != null
          ) {
            this.formValue["mapName"] = RES.nodeName;
          }
          if (RES.lcid != "" && RES.lcid != undefined && RES.lcid != null) {
            this.formValue["lcid"] = RES.lcid;
          }
          this.formValue["layerId"] = RES.layerId;
          if (
            RES.mapPixelX != "" &&
            RES.mapPixelX != undefined &&
            RES.mapPixelX != null
          ) {
            this.formValue["mapPixelX"] = RES.mapPixelX;
          }
          if (
            RES.mapPixelY != "" &&
            RES.mapPixelY != undefined &&
            RES.mapPixelY != null
          ) {
            this.formValue["mapPixelY"] = RES.mapPixelY;
          }
          if (
            RES.mapActualY != "" &&
            RES.mapActualY != undefined &&
            RES.mapActualY != null
          ) {
            this.formValue["mapActualY"] = RES.mapActualY;
          }
          if (
            RES.mapActualX != "" &&
            RES.mapActualX != undefined &&
            RES.mapActualX != null
          ) {
            this.formValue["mapActualX"] = RES.mapActualX;
          }

          this.formValue["mapOriginX"] = RES.mapOriginX;
          this.formValue["mapOriginY"] = RES.mapOriginY;
          if (
            RES.remark != "" &&
            RES.remark != undefined &&
            RES.remark != null
          ) {
            this.formValue["remark"] = RES.remark;
          }
          if (
            RES.isDefault != "" &&
            RES.isDefault != undefined &&
            RES.isDefault != null
          ) {
            this.switchVal = RES.isDefault + "";
          } else {
            this.switchVal = "0";
          }
        });
    },
    /**************************************************************/
    //阻止upload的自己上传，进行再操作
    beforeupload(file) {
      const fileNameArr = file.name.split(".");
      const typeName = fileNameArr[fileNameArr.length - 1].toLocaleLowerCase();
      if (typeName != "jpg" && typeName != "png" && typeName != "jpeg") {
        this.$message("上传文件只能是 jpg、png、jpeg格式!");
        return false;
      }

      //创建临时的路径来展示图片
      let windowURL = window.URL || window.webkitURL;
      this.imgSrc = windowURL.createObjectURL(file);

      this.file.append("file", file);
      return false;
    },

    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //表单提交的事件
          this.file.append("idx", this.mapId);
          this.file.append("type", "layer");
          this.file.append("nodeName", this.nodeName);
          this.file.append("parentId", this.parentId);
          this.file.append("mapName", this.formValue.mapName);
          this.file.append("lcid", this.formValue.lcid);
          this.file.append("layerId", this.formValue.layerId);
          this.file.append("mapPixelX", this.formValue.mapPixelX);
          this.file.append("mapPixelY", this.formValue.mapPixelY);
          this.file.append("mapActualY", this.formValue.mapActualY);
          this.file.append("mapActualX", this.formValue.mapActualX);
          this.file.append("mapOriginX", this.formValue.mapOriginX);
          this.file.append("mapOriginY", this.formValue.mapOriginY);
          this.file.append("remark", this.formValue.remark);
          this.file.append("isDefault", this.switchVal);
          let that = this;
          this.$api.mapmanage.mapDefault().then((res) => {
            if (res.code === 400) {
              that.$api.mapmanage
                .saveAndEditMap(that.file, {
                  "Content-Type":
                    "multipart/form-data;boundary = " + new Date().getTime(),
                })
                .then((res) => {
                  if (res.code == 500) {
                    that.file = new FormData();
                    that.v_MyMessage(res.msg, "warning");
                  } else if (res.code == 200) {
                    that.file = new FormData();
                    that.v_MyMessage(
                      this.$t("Notice.mapAddedSuccessfully"),
                      "success"
                    );
                  }
                })
                .catch((error) => {
                  that.file = new FormData();
                });
            } else if (res.code === 200) {
              that.defaultMapVisible = true;
            }
          });
        } else {
          this.v_MyMessage(this.$t("Notice.verificationFailed"));
          return false;
        }
      });
    },
    defaultMap() {
      this.$api.mapmanage
        .saveAndEditMap(this.file, {
          "Content-Type":
            "multipart/form-data;boundary = " + new Date().getTime(),
        })
        .then((res) => {
          if (res.code == 500) {
            this.file = new FormData();
            this.v_MyMessage(res.msg, "warning");
            this.defaultMapVisible = false;
          } else if (res.code == 200) {
            this.file = new FormData();
            this.defaultMapVisible = false;
            this.v_MyMessage(this.$t("Notice.mapAddedSuccessfully"), "success");
          }
        })
        .catch((error) => {
          this.file = new FormData();
        });
    },
    //添加地图
    append() {
      this.editEnable = true;
      this.editVisible = true;
      if (
        this.mapId === "" ||
        this.mapId === null ||
        this.mapId === undefined
      ) {
        this.mapId = 0;
        this.nodeName = "";
        this.deptForm = { ParentNode: "" };
      } else {
        this.deptForm = { ParentNode: this.nodeName };
      }
    },
    //编辑
    edit(data) {
      this.editVisible = true;
      this.editEnable = false;
      this.nodeName = data.label;
      this.parentId = data.parentId;
      this.idxNode = data.id;
      if (data.label) {
        this.deptForm["childenNode"] = data.label;
      } else {
        this.deptForm["childenNode"] = "";
      }
      if (data.parentName) {
        this.deptForm["ParentNode"] = data.parentName;
      } else {
        this.deptForm["ParentNode"] = "";
      }
    },
    //确定添加
    saveEdit(formName) {
      let that = this;
      that.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editEnable) {
            let param = {
              parentId: that.mapId,
              nodeName: that.deptForm.childenNode,
            };
            that.$api.mapmanage.addMapNode(param).then((res) => {
              let RES = that.responseUse(res, true);
              if (RES) {
                that.editVisible = false;
                that.getAreaTree();
              }
            });
          } else {
            let param = {
              parentId: this.parentId,
              nodeName: this.deptForm.childenNode,
              idx: this.idxNode,
            };
            this.$api.mapmanage.addMapNode(param).then((res) => {
              let RES = this.responseUse(res, true);
              if (RES) {
                this.editVisible = false;
                this.getAreaTree();
              }
            });
          }
        } else {
          this.v_MyMessage(this.$t("Notice.verificationFailed"));
          return false;
        }
      });
    },
    //删除地图
    remove(node, data) {
      this.delVisible = true;
      this.idxMap = [{ idx: data.id }];
    },
    //  确定删除
    delMapTree() {
      this.$api.mapmanage.deleteMap(this.idxMap).then((res) => {
        let RES = this.responseUse(res, true);
        if (RES) {
          this.getAreaTree();
          this.delVisible = false;
          this.formValue.mapurl = "";
          this.formValue.mapName = "";
          this.formValue.lcid = "";
          this.formValue.layerId = null;
          this.formValue.mapPixelX = null;
          this.formValue.mapPixelY = null;
          this.formValue.mapActualX = null;
          this.formValue.mapOriginX = null;
          this.formValue.mapOriginY = null;
          this.formValue.remark = "";
        }
      });
    },
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>

#upload >>> .el-upload-dragger {
  width: 100%;
  height: 80vh;
}
#upload >>> .el-upload--text {
  background-color: #fff;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  box-sizing: border-box;
  width: 100%;
  height: 80vh;
  text-align: center;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

#upload >>> .el-upload--text .el-icon-upload {
  font-size: 67px;
  color: #97a8be;
  width: 100%;
  margin: 40px 0 16px;
  line-height: 50px;
}

#upload >>> .el-upload--text {
  color: #97a8be;
  font-size: 14px;
  width: 100%;
  text-align: center;
}

#upload >>> .el-upload--text em {
  font-style: normal;
}

/* .custom-tree-node span:nth-child(2){
    display:inline-block;
    position: absolute;
    right: 0;
    font-size: 14px;
  } */

.contentText {
  width: 100%;
  .content1 {
  margin-right: 380px;
}
.content2 {
  position: absolute;
  top: 20px;
  right: 17px;
  width: 300px;
  padding: 0 20px;
  border: 1px solid #dcdfee;
  background-color: #fff;
  height: 100%;
  overflow-x: hidden;
}

}
.custom-tree-node {
  width: 100%;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 48px;
  color: #8c939d;
  line-height: 415px;
  text-align: center;
}
.avatar {
  display: block;
}


.submitBtn {
  position: absolute;
  top: 10px;
  right: 55px;
  z-index: 0;
}
.el-tree-node__content {
  font-size: 14px;
  height: 40px;
  font-weight: 400;
}
.el-tree-node__label:hover {
  color: #0593d3;
}
.el-tree-node:focus > .el-tree-node__content {
  color: #0593d3;
}
.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  color: red;
  background-color: transparent;
}
</style>